#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using GameStateManagement.Mine;
using GameStateManagement.GameHelper;
using Microsoft.Xna.Framework.Media;
#endregion

namespace GameStateManagement
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        MyMenuEntry soundEnableEntry;
        MyMenuEntry audioEnableEntry;
        MyMenuEntry volumeMenuEntry;
        MyMenuEntry fullscreenEntry;

        enum Ungulate
        {
            BactrianCamel,
            Dromedary,
            Llama,
        }

        static Ungulate currentUngulate = Ungulate.Dromedary;

        static string[] languages = { "Easy", "Normal", "Hard" };
        static int currentLanguage = 0;

        static bool frobnicate = true;

        static int vol = 23;
        private MyMenuEntry autoSaveEntry;

        #endregion


        public override void UnloadContent()
        {
            
            base.UnloadContent();
            GameSettings.Instance.Save();
        }

        public override void LoadContent()
        {
            base.LoadContent();
            SetMenuEntryText();
        }

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen()
            : base("Options")
        {
            // Create our menu entries.
            //GameSettings.Instance.Load();
            soundEnableEntry = new MyMenuEntry(string.Empty);
            audioEnableEntry = new MyMenuEntry(string.Empty);
            volumeMenuEntry = new MyMenuEntry(string.Empty);
            fullscreenEntry = new MyMenuEntry("");
            autoSaveEntry = new MyMenuEntry("");
            MyMenuEntry back = new MyMenuEntry("Back");

            // Hook up menu event handlers.
            soundEnableEntry.Selected += SoundEnableMenuEntrySelected;
            audioEnableEntry.Selected += AudioMenuEntrySelected;
            volumeMenuEntry.Selected += VolumeMenuEntrySelected;
            back.Selected += OnCancel;
            fullscreenEntry.Selected += new System.EventHandler<MyEventArgs>(fullscreenEntry_Selected);
            autoSaveEntry.Selected += new System.EventHandler<MyEventArgs>(autoSaveEntry_Selected);
            // Add entries to the menu.
            MenuEntries.Add(autoSaveEntry);
            MenuEntries.Add(soundEnableEntry);
            MenuEntries.Add(audioEnableEntry);
            MenuEntries.Add(volumeMenuEntry);
            MenuEntries.Add(fullscreenEntry);
            MenuEntries.Add(back);

            MenuEntries.SetColor(Color.Red, Color.Black);
         
        }

        void autoSaveEntry_Selected(object sender, MyEventArgs e)
        {
            GameSettings.Instance.AutoSave = !GameSettings.Instance.AutoSave;
            SetMenuEntryText();
        }

        protected override void OnCancel(PlayerIndex playerIndex)
        {
            GameSettings.Instance.Save();
            base.OnCancel(playerIndex);
        }

        void fullscreenEntry_Selected(object sender, MyEventArgs e)
        {
            ScreenManager.Graphics.ToggleFullScreen();
            GameSettings.Instance.IsFullScreen = ScreenManager.Graphics.IsFullScreen;

            SetMenuEntryText();
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            autoSaveEntry.Text = "Auto Save: " + (GameSettings.Instance.AutoSave ? "true" : "false");
            soundEnableEntry.Text = "Sound Effect: " + (GameSettings.Instance.SoundEnable ? "on" : "off");
            audioEnableEntry.Text = "Music: " + (GameSettings.Instance.AudioEnable ? "on" : "off");
            volumeMenuEntry.Text = "Volume: " + vol;
            fullscreenEntry.Text = "Fullscreen: " + (ScreenManager.Graphics.IsFullScreen ? "Yes" : "No");        
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Ungulate menu entry is selected.
        /// </summary>
        void UngulateMenuEntrySelected(object sender, MyEventArgs e)
        {
            currentUngulate++;

            if (currentUngulate > Ungulate.Llama)
                currentUngulate = 0;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Language menu entry is selected.
        /// </summary>
        void SoundEnableMenuEntrySelected(object sender, MyEventArgs e)
        {
            bool cur = GameSettings.Instance.SoundEnable;
            GameSettings.Instance.SoundEnable = !cur;
            
            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Frobnicate menu entry is selected.
        /// </summary>
        void AudioMenuEntrySelected(object sender, MyEventArgs e)
        {
            GameSettings.Instance.AudioEnable = !GameSettings.Instance.AudioEnable;
            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Pause();
            else
                if (MediaPlayer.State == MediaState.Paused)
                    MediaPlayer.Resume();
            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Elf menu entry is selected.
        /// </summary>
        void VolumeMenuEntrySelected(object sender, MyEventArgs e)
        {
            vol++;

            SetMenuEntryText();
        }

        

        #endregion
    }
}
